package com.firefightingiot.firefightingiot.web.service;

import com.firefightingiot.firefightingiot.common.base.BaseService;
import org.eclipse.californium.core.CoapResource;
import org.eclipse.californium.core.CoapServer;
import org.eclipse.californium.core.coap.CoAP;
import org.eclipse.californium.core.server.resources.CoapExchange;

//@Service
public class Test extends BaseService {

    public void getCoapServer() {
        CoapServer server = new CoapServer();//主机为localhost 端口为默认端口5683
        server.add(new CoapResource("data") {//创建一个资源为hello 请求格式为 主机：端口\hello

            @Override
            public void handleGET(CoapExchange exchange) { //重写处理GET请求的方法
                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
            }

            @Override
            public void handlePOST(CoapExchange exchange) {
                System.out.println("post start");
                String result = exchange.getRequestText();
                System.out.println("the received text:" + result);
                //exchange.respond(ResponseCode.CHANGED);
                exchange.respond(CoAP.ResponseCode.CONTENT, exchange.getRequestText());
            }

        });
    }
}
